package com.module.product.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.product.dao.ProductGroupDao;
import com.module.product.vo.ProductGroup;

/**
*
* 商品组合 业务处理类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品属性组]
*/
public class ProductGroupManager extends GenericHibernateManager<ProductGroup, String>{
	private static final ProductGroupManager instance = new ProductGroupManager();
	
	static Logger logger = Logger.getLogger(ProductGroupManager.class.getName());
	private ProductGroupManager(){}
	
	public synchronized static ProductGroupManager getInstance() {
		return instance;
	}
	
    ProductGroupDao dao = ProductGroupDao.getInstance();
    /*
    public void add(ProductBeanUnit beanUnit) throws Exception
    {
    	ProductGroup obj = beanUnit.getProductGroup();
    	String id = obj.getId();
    	boolean isTrue = false;
        if(StringUtils.isBlank(obj.getId())){//添加
            obj.setStatus(CUDEnum.C.toString());
            obj.setCreateTime(new Date());
        }else{
            obj.setStatus(CUDEnum.U.toString());
            obj.setUpdateTime(new Date());
        }
        isTrue =  saveOrUpdate(obj);
        Map statusMap = addOptReturnMessage(id, isTrue);
		beanUnit.setResult(statusMap);
   
    }
    public boolean delete(ProductBeanUnit beanUnit) throws Exception
    {
    	ProductGroup obj = beanUnit.getProductGroup();
    	Session session =HibernateSessionFactory.getSession();
    	Transaction tx =session.beginTransaction();
    	boolean flag =false;
    	try{
    		flag = dao.deleteObjs(session,obj);
    		tx.commit();
    	}catch(Exception e){
            if (tx != null) {
                tx.rollback();
            }
            throw e;
    		
    	}finally{
    		this.closeSession();
    	}
        return flag;
    }
    */
    public boolean update(ProductGroup obj) throws Exception
    {
    	Session session = HibernateSessionFactory.getSession();
    	boolean flag = false;
    	try{
    		flag =  dao.update(session,obj);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.closeSession();
    	}
    	return flag;
    }
    
    public Page getList(ProductGroup obj,Page page) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	Page pagePro = null;
    	try{
    		pagePro =  dao.getList(session,obj,page);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.closeSession();
    	}
        return pagePro;
    }
    
    public List<ProductGroup> listAll() throws Exception{
    	Session session =HibernateSessionFactory.getSession();
        List<ProductGroup> list  = new ArrayList();
    	try{
    	    	list = dao.listAll(session);
	    	}catch(Exception e){
	    		throw e;
	    		
	    	}finally{
	    		this.closeSession();
	    	}
        return list;
    }
    /**
	 * <公用添加和修改操作> <功能详细描述>
	 * 
	 * @param beanUnit
	 * @param isTrue
	 * @see [类、类#方法、类#成员]
	 */
	private Map addOptReturnMessage(String id,boolean isTrue) {
		Map<String ,Object> statusMap = new HashMap<String ,Object>();
		String status = null;
		if (StringUtils.isBlank(id)) {
			statusMap.put("state", isTrue);
			status = (isTrue == true ? "添加成功！" : "添加失败！");
			statusMap.put("msg", status);
		} else {
			statusMap.put("state", isTrue);
			status = (isTrue == true ? "修改成功！" : "修改失败！");
			statusMap.put("msg", status);
		}
		return statusMap;

	}

	@Override
	public GenericHibernateDao<ProductGroup, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
}
